package cool.tianyou.blue.bird.rest;

import cool.tianyou.blue.bird.common.exception.RestApiException;
import cool.tianyou.blue.bird.common.model.entity.RestApiResult;
import cool.tianyou.blue.bird.common.model.entity.ScanResult;
import cool.tianyou.blue.bird.common.model.vo.UserVO;
import cool.tianyou.blue.bird.common.util.RestResultUtil;
import cool.tianyou.blue.bird.module.auth.model.dto.LoginDTO;
import cool.tianyou.blue.bird.module.auth.service.UserService;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Liutianyou
 * @date 2022/2/7 3:57 下午
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/account")
public class AccountController {

  private final UserService userService;

  @PostMapping("/login")
  public ResponseEntity<RestApiResult<LoginDTO>> login(@Validated @RequestBody UserVO userVO,
      BindingResult result) {
    if (result.hasErrors()) {
      throw new RestApiException(result.getAllErrors().get(0).getDefaultMessage());
    }
    LoginDTO login = userService.login(userVO);
    return RestResultUtil.ok(login);
  }

  @GetMapping("/logout")
  public ResponseEntity<RestApiResult<String>> logout() {
    userService.logOut();
    return RestResultUtil.ok("");
  }


  @PostMapping("/register")
  public ResponseEntity<RestApiResult<Object>> register(@Validated @RequestBody UserVO userVO,
      BindingResult result) {
    if (result.hasErrors()) {
      throw new RestApiException(result.getAllErrors().get(0).getDefaultMessage());
    }
    return RestResultUtil.ok(userService.register(userVO));
  }

  @PostMapping("/auth")
  public ResponseEntity<RestApiResult<String>> auth(String token) {
    userService.authentication(token);
    return RestResultUtil.ok("ok");
  }

  @GetMapping("/qrcode/login")
  public ResponseEntity<RestApiResult<Map<Object, Object>>> qrcode(String deviceUid, String brand,
      String type) {
    return RestResultUtil.ok(userService.qrcode(deviceUid, brand, type));
  }

  @GetMapping("/qrcode/info/{uuid}")
  public ResponseEntity<RestApiResult<ScanResult>> qrcodeInfo(@PathVariable("uuid") String uuid) {
    return RestResultUtil.ok(userService.qrcodeInfo(uuid));
  }

  @GetMapping("/qrcode/scan/{uuid}")
  public ResponseEntity<RestApiResult<ScanResult>> getQrcodeInfo(
      @PathVariable("uuid") String uuid) {
    return RestResultUtil.ok(userService.qrcodeInfo(uuid));
  }

  @GetMapping("/confirm/{uuid}")
  public ResponseEntity<RestApiResult<LoginDTO>> confirm(@PathVariable("uuid") String uuid) {
    return RestResultUtil.ok(userService.loginConfirm(uuid));
  }
}
